package com.service;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dao.IXpinqmsgDao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_msg;
import com.tx.XpinqMsg.XpinqmsgI;
import com.tx.XpinqMsg.XpinqmsgO;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpinqmsgService")
public class XpinqmsgService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XpinqmsgService.class);
	@Autowired
	private XpinqmsgI xpinqmsgI;
	
	@Autowired
	private XpinqmsgO xpinqmsgO; 
	
	@Autowired
	private IXpinqmsgDao inqmsgDao;
	
	@Autowired
	private MqOpe mqOpe;
	
	public String subMainProc(String strInMsg){
		List<Ret_msg> retWorList = new ArrayList<Ret_msg>();
		xpinqmsgI = (XpinqmsgI) mqOpe.inStr2Obj(strInMsg,xpinqmsgI);
		logger.info("InTrx is "+ strInMsg );
		char action_flgAry[] = xpinqmsgI.getAction_flg().toCharArray();
		char action_flg = action_flgAry[0]; 
		switch ( action_flg ){
			case 'I': 
				xpinqmsgO = findMsgByMsgNum(  xpinqmsgI.getMsg_id() );
				break ;
			default:
				logger.info( "mapping no action_flg ,error");
		}
		
		logger.info(mqOpe.obj2Str( xpinqmsgO ));
		return mqOpe.obj2Str( xpinqmsgO );
	}
	
	public XpinqmsgO findMsgByMsgNum( String msg_num){
		Ret_msg retMsg = new Ret_msg();
		XpinqmsgO xpinqmsgO = new XpinqmsgO();
		xpinqmsgO.setTrx_id("XPINQMSG");
		xpinqmsgO.setType_id("O");
		try{
			
		}catch(Exception ex){
			logger.error(Util.stackTraceToString(ex));

			xpinqmsgO.setRtn_code("0000001");
			xpinqmsgO.setRtn_mesg(ex.toString());
		}
		String querySql=" from Ret_msg where id.msg_id= '"+ msg_num +"'";
		List<Ret_msg> retMsgList = inqmsgDao.find( querySql );
		
		if( retMsgList.size()>0 ){
			retMsg = retMsgList.get( 0 );
			xpinqmsgO.setRtn_code("0000000");
			xpinqmsgO.setRtn_mesg("success");
			xpinqmsgO.setMsg_id(retMsg.getId().getMsg_id());
			xpinqmsgO.setMsg_body(retMsg.getMsg_body());
			xpinqmsgO.setAct_body(retMsg.getAct_body());
			xpinqmsgO.setAct_dtl(retMsg.getAct_dtl());
		}else{
			xpinqmsgO.setRtn_code("0000002");
			xpinqmsgO.setRtn_mesg("not found record from Tbl[RET_WO] where msg_id=" + msg_num);
		}
		return xpinqmsgO ;
	}
}
